//
//  AllOrdersListViewController.m
//  LuxuryGoods
//
//  Created by DCQ on 2018/5/26.
//  Copyright © 2018年 Future. All rights reserved.
//

#import "AllOrdersListViewController.h"

#import "OrderDetailViewController.h"
#import "UserInfoModel.h"
#import "OrderListModel.h"
#import "LGOrderSectionCell.h"
#import "LGOrderGoodsCell.h"
#import "LGOrderBottomCell.h"
#import "LGOrderMultipleCell.h"
#import "LGCommentViewController.h"
#import "OrderLogisticsViewController.h"

NSString * const LGOrderSectionCellIdentifier = @"LGOrderSectionCellIdentifier";
NSString * const LGOrderGoodsCellIdentifier = @"LGOrderGoodsCellIdentifier";
NSString * const LGOrderMultipleCellIdentifier = @"LGOrderMultipleCellIdentifier";
NSString * const LGOrderBottomCellIdentifier = @"LGOrderBottomCellIdentifier";
@class XLPagerTabStripViewController;
@interface AllOrdersListViewController ()
@property(nonatomic,assign) AllOrdersListStyle style;
@property(nonatomic,strong) OrderListModel * listModel;
@end

@implementation AllOrdersListViewController
- (instancetype)initWithStyle:(AllOrdersListStyle )style{
    
    self = [super init];
    if (self) {
        self.style = style;
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getNormalTableViewNetworkData) name:@"UPDATE_ORDER_LIST" object:nil];
}

- (void)updateList:(id)sender{
    
    [self requestOrder];
}
- (void)requestOrder{
    NSMutableDictionary * params = [NSMutableDictionary dictionary];
    [params addEntriesFromDictionary:@{@"token":UserInfo.userToken}];
    switch (self.style) {
        case AllOrdersListStyle_all:
            
            break;
        case AllOrdersListStyle_waitSend:
            [params addEntriesFromDictionary:@{@"status":@"10"}];
            break;
        case AllOrdersListStyle_waitGoods:
             [params addEntriesFromDictionary:@{@"status":@"20"}];
            break;
        case AllOrdersListStyle_waitComment:
            [params addEntriesFromDictionary:@{@"status":@"30"}];
            break;
        case AllOrdersListStyle_waitPayment:
            [params addEntriesFromDictionary:@{@"status":@"0"}];
            break;
            
        case AllOrdersListStyle_refundReview:
             [params addEntriesFromDictionary:@{@"status":@"50"}];
            break;
        
        default:
            break;
    }
    [params addEntriesFromDictionary:@{@"p":@(self.currentPageNo)}];
    [params addEntriesFromDictionary:@{@"psize":@(self.pageCount)}];
    WEAKSELF
    [[MainApi sharedInstance] postPath:@"/Order/getList" params:params resultBlock:^(NSDictionary *responseObject, NSError *error) {
        STRONGSELF
        NSLog(@"%@===",responseObject);
        if (responseObject && !error) {
            if (strongSelf.currentPageNo == 0) {
                strongSelf.listModel = nil;
                 strongSelf.listModel = [[OrderListModel alloc]initWithDictionary:responseObject error:nil];
                 strongSelf.currentPageNo ++;
            }else{
                if (responseObject[@"data"] && [responseObject[@"data"] count] > 0 ) {
                    [strongSelf.listModel.data addObjectsFromArray: responseObject[@"data"]];
                    strongSelf.currentPageNo ++;
                }
            }
            
        }
        [strongSelf updateTableView];
    }];
}
- (void)registerCell{
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([LGOrderSectionCell class]) bundle:nil] forCellReuseIdentifier:LGOrderSectionCellIdentifier];
    
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([LGOrderGoodsCell class]) bundle:nil] forCellReuseIdentifier:LGOrderGoodsCellIdentifier];
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([LGOrderMultipleCell class]) bundle:nil] forCellReuseIdentifier:LGOrderMultipleCellIdentifier];
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([LGOrderBottomCell class]) bundle:nil] forCellReuseIdentifier:LGOrderBottomCellIdentifier];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (NSString *)titleForPagerTabStripViewController:(XLPagerTabStripViewController *)pagerTabStripViewController{
    NSString * tabTitle = @"";
    switch (self.style) {
        case AllOrdersListStyle_all:
            tabTitle = @"全部";
            break;
        case AllOrdersListStyle_waitPayment:
            tabTitle = @"待付款";
            break;
        case AllOrdersListStyle_waitSend:
            tabTitle = @"待发货";
            break;
        case AllOrdersListStyle_waitGoods:
            tabTitle = @"待收货";
            break;
        case AllOrdersListStyle_waitComment:
            tabTitle = @"待评价";
            break;
        case AllOrdersListStyle_refundReview:
            tabTitle = @"退款审核中";
            break;
        default:
            break;
    }
    return   tabTitle;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    NSInteger section = 0;
    section = [self.listModel.data count];
    return section;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSInteger rows = 3;
    return rows;
}



- (CGRect)getTableViewFrame{
    CGRect tableViewFrame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 44 );
    return tableViewFrame;
}
- (BOOL)isAddRefreshHeader{
    return YES;
}
- (BOOL)isAddRefreshFooter{
    return YES;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    CGFloat  height = 0;
    if (indexPath.row == 0) {
        height = 50;
    }else if (indexPath.row == 1){
        height = 70;
    }else if (indexPath.row == 2){
        height = 50;
    }
    return height;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell * cell = nil;
     OrderModel * model = self.listModel.data[indexPath.section];
    if (indexPath.row == 0) {
        LGOrderSectionCell * tempCell = [tableView dequeueReusableCellWithIdentifier:LGOrderSectionCellIdentifier];
        
        [tempCell setupOrderData:model withType:ViewSectionType_AllOrders];
        cell = tempCell;
    }else if(indexPath.row == 1){
    
        if ([model.goods_item count] >1) {
            LGOrderMultipleCell * tempCell = [tableView dequeueReusableCellWithIdentifier:LGOrderMultipleCellIdentifier];
            [tempCell setupOrderData:model];
            cell = tempCell;
        }else{
            LGOrderGoodsCell * tempCell = [tableView dequeueReusableCellWithIdentifier:LGOrderGoodsCellIdentifier];
            [tempCell setupOrderData:model];
            cell = tempCell;
        }
        
    }else if (indexPath.row == 2){
        LGOrderBottomCell * tempCell = [tableView dequeueReusableCellWithIdentifier:LGOrderBottomCellIdentifier];
        [tempCell setupOrderData:model withViewType:ViewType_AllOrders];
        tempCell.btnBlock = ^(OrderBottomStyle style, NSString *orderId) {
            switch (style) {
                case OrderBottomStyle_waitPayment:
                    [self gotoPaymentVC:orderId];
                    break;
                case OrderBottomStyle_waitComment:
                    [self gotoCommentVC:orderId];
                    break;
                case OrderBottomStyle_waitGoods:
                    [self confirmGoods:orderId];
 
                    break;
                default:
                    break;
            }
        };
        cell = tempCell;
    }
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [UIView new];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 10;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
 
    NSString * orderId = @"";
    OrderModel * model = self.listModel.data[indexPath.section];
    NSInteger  status  = [model.status integerValue];
    OrderDetailStyle detailStyle = OrderDetailStyle_normal;
    detailStyle = [self getStatus:status];
    orderId =[NSString stringWithFormat:@"%@",model.order_id];
    OrderDetailViewController * detailVC = [[OrderDetailViewController alloc]initWithStype:detailStyle withOrderId:orderId];
    [self pushViewController: detailVC];
}
- (OrderDetailStyle  )getStatus:(NSInteger )status{
     OrderDetailStyle detailStyle = OrderDetailStyle_normal;
   
    //订单状态 0-待付款 10-待发货 20-待收货 30-待评价 40-已完成 50-退款中 51-退款被拒绝 60 -退款成功 90-已取消
    switch (status) {
        case 0:
//            stateStr = @"待付款";
            detailStyle = OrderDetailStyle_waitPayment;
            break;
        case 10:
//            stateStr = @"待发货";
            detailStyle = OrderDetailStyle_waitSend;
            break;
        case 20:
//            stateStr = @"待收货";
            detailStyle = OrderDetailStyle_waitGoods;
            break;
        case 30:
//            stateStr = @"待评价";
            detailStyle = OrderDetailStyle_waitComment;
            break;
        case 40:
//            stateStr = @"已完成";
            detailStyle = OrderDetailStyle_complete;
            
            break;
        case 50:
//            stateStr = @"退款中";
            detailStyle = OrderDetailStyle_refundReview;
            break;
        case 51:
//            stateStr = @"退款被拒绝";
             detailStyle = OrderDetailStyle_rejectRefund;
            break;
        case 60:
//            stateStr = @"退款成功";
             detailStyle = OrderDetailStyle_completeRefund;
            
            break;
        case 90:
//            stateStr = @"取消";
             detailStyle = OrderDetailStyle_cancel;
            break;
        default:
            break;
    }
    return detailStyle;
}

//   页面加载调用 子类重写
- (void)getNormalTableViewNetworkData{
    NSLog(@"进入页面请求 子类重写");
    self.currentPageNo = 0;
    self.pageCount = 10;
  
    [self requestOrder];
}
- (void)getLoadMoreTableViewNetworkData{
    
     [self requestOrder];
}
- (NSInteger )getNetworkTableViewDataCount{
    
    return  [self.listModel.data count];
}

- (void)gotoCommentVC:(NSString *) orderId{
    
    LGCommentViewController * commentVC = [[LGCommentViewController alloc]initWithOrderId:orderId  withVCType:LGCommentVCType_order];
    [self pushViewController:commentVC];
}


- (void)gotoPaymentVC:(NSString *) orderId{
   
    
}

- (void)confirmGoods:(NSString *)orderId{
    
    [self showAlertView:@"确定已经收到货了吗?" withConfirmAction:^(UIAlertAction *action) {
        [self requestCancelOrder:@"40" withOrderId:orderId];
    } withCancelAction:^(UIAlertAction *action) {
        
    }];
}
//
- (void)requestCancelOrder:(NSString *)status withOrderId:(NSString *)orderId{
    NSDictionary * params = @{@"order_id":orderId,@"token":UserInfo.userToken,@"status":status};
    
    [[MainApi sharedInstance] postPath:@"/Order/confirmOrCancel" params:params resultBlock:^(NSDictionary *responseObject, NSError *error) {
        
        NSLog(@"%@===",responseObject);
        if (!error) {
             [self requestOrder];
        }
        
    }];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
